package cn.darkhorse.messystem.service.impl;

import cn.darkhorse.messystem.mapper.SaasTeamMapper;
import cn.darkhorse.messystem.mapper.SaasTeamUserMapper;
import cn.darkhorse.messystem.pojo.SaasTeam;
import cn.darkhorse.messystem.pojo.SaasTeamUser;
import cn.darkhorse.messystem.service.SaasTeamService;
import cn.darkhorse.messystem.service.SaasTeamUserService;
import cn.darkhorse.messystem.vo.SaasTeamUserVO;
import cn.darkhorse.messystem.vo.SaasTeamVO;
import cn.darkhorse.messystem.vo.SysUserVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 班组表 服务实现类
 * </p>
 *
 * @author maoguanjun
 * @since 2024-04-27
 */
@Service
public class SaasTeamServiceImpl extends ServiceImpl<SaasTeamMapper, SaasTeam> implements SaasTeamService {
    @Resource
    private SaasTeamUserService saasTeamUserService;
    @Resource
    private SaasTeamUserMapper saasTeamUserMapper;
    @Resource
    private SysUserMapper userMapper;
    /**
     * 查看班组
     *
     * @param teamId
     * @return
     */
    @Override
    public SaasTeamVO view(Long teamId) {
        SaasTeamVO vo=new SaasTeamVO();
        SaasTeam byId = getById(teamId);
        if(null!=byId) {
            BeanUtils.copyProperties(byId, vo);
        }
        LambdaQueryWrapper<SaasTeamUser> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SaasTeamUser::getTeamId,teamId);
        List<SaasTeamUser> list = saasTeamUserService.list(wrapper);
        vo.setUserList(list);
        return vo;
    }

    /**
     * 删除班组
     *
     * @param teamId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long teamId) {
        //TODO 校验是否被引用
        boolean b = removeById(teamId);
        LambdaQueryWrapper<SaasTeamUser> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SaasTeamUser::getTeamId,teamId);
        boolean remove = saasTeamUserService.remove(wrapper);
        return b&&remove;
    }

    /**
     * 保存
     *
     * @param vo
     * @return
     */
    @Override
    public boolean saveSaasTeam(SaasTeam vo) {
//        if(StringUtils.isBlank(vo.getCode())){
//            throw new ServiceException("编码不能为空！");
//        }
        if(StringUtils.isBlank(vo.getName())){
            throw new ServiceException("名称不能为空！");
        }
        LambdaQueryWrapper<SaasTeam> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SaasTeam::getCode,vo.getCode());
        LambdaQueryWrapper<SaasTeam> nameWrapper=new LambdaQueryWrapper<>();
        nameWrapper.eq(SaasTeam::getName,vo.getName());
        if(null!=vo.getId()){
            wrapper.ne(SaasTeam::getId,vo.getId());
            nameWrapper.ne(SaasTeam::getId,vo.getId());
        }
        long count = count(wrapper);
        long count1 = count(nameWrapper);
//        if(count>0){
//            throw new ServiceException("编码已存在！");
//        }
        if(count1>0){
            throw new ServiceException("名称已存在！");
        }
        boolean b = saveOrUpdate(vo);
        return b;
    }

    /**
     * 列表
     *
     * @return
     */
    @Override
    public List<SaasTeamVO> listAll() {
        List<SaasTeamVO> list = saasTeamUserMapper.listAll();
        return list;
    }

    /**
     * 关联用户
     *
     * @param vo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addJoinUser(SaasTeamVO vo) {
        LambdaQueryWrapper<SaasTeamUser> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SaasTeamUser::getTeamId,vo.getId());
        saasTeamUserService.remove(wrapper);
        boolean b = saasTeamUserService.saveBatch(vo.getUserList());
        return b;
    }

    /**
     * 根据班组获取用户
     *
     * @param teamId
     * @return
     */
    @Override
    public List<SaasTeamUserVO> getUserNameByTeamId(Long teamId) {
        LambdaQueryWrapper<SaasTeamUser> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SaasTeamUser::getTeamId,teamId);
        List<SaasTeamUser> list = saasTeamUserService.list(wrapper);
        List<SysUser> sysUsers = userMapper.selectUserListNoAuth(new SysUser());
        Map<Long, SysUser> collect = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
        List<SaasTeamUserVO> vos=new ArrayList<>();
        if(null!=list&&list.size()>0){
            list.forEach(item->{
                SaasTeamUserVO vo=new SaasTeamUserVO();
                BeanUtils.copyProperties(item,vo);
                if(collect.containsKey(item.getUserId())){
                    vo.setUserName(collect.get(item.getUserId()).getNickName());
                }
                vos.add(vo);
            });
        }
        return vos;
    }

    /**
     * 查询班组关联用户
     *
     * @param userName
     * @param userPhone
     * @param teamName
     * @return
     */
    @Override
    public List<SysUserVO> getUserList(String userName, String userPhone, String teamName) {
        if(StringUtils.isBlank(teamName)){
            throw new ServiceException("缺失班组！");
        }
        List<SysUserVO> list=saasTeamUserMapper.getUserList(userName,userPhone,teamName);
        return list;
    }

    @Override
    public SysUserVO getUserByUserId(Long userId) {
        return saasTeamUserMapper.getUserByUserId(userId);
    }

    @Override
    public List<SysUserVO> getUsersByUserId(List<Long> userId) {
        return saasTeamUserMapper.getUserByUserIds(userId);
    }

}
